﻿using PC.AzureMultiApp.Core.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.IO;

namespace Core.Test
{
    
    
    /// <summary>
    ///This is a test class for FileHelperTest and is intended
    ///to contain all FileHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class FileHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod()]
        [DeploymentItem("FileHelper\\sample.zip", "FileHelper")]
        public void UnzipTest()
        {
            string zipFile = testContextInstance.DeploymentDirectory + "\\FileHelper\\sample.zip";
            
            string targetFolder = Path.Combine(testContextInstance.DeploymentDirectory, "targetFolder");
            if (Directory.Exists(targetFolder))
                Directory.Delete(targetFolder, true);
            Directory.CreateDirectory(targetFolder);

            bool doBackup = false;
            bool actual = FileHelper.Unzip(zipFile, targetFolder, doBackup);

            Assert.IsTrue(actual);
            Assert.IsTrue(Directory.GetFiles(targetFolder).Count() == 1);

            Directory.Delete(targetFolder, true);
        }

        [TestMethod()]
        [DeploymentItem("FileHelper\\sample.zip", "FileHelper")]
        [ExpectedException(typeof(System.ArgumentException))]
        public void UnzipArgumentException_FolderNoExists_Test()
        {
            string zipFile = testContextInstance.DeploymentDirectory + "\\FileHelper\\sample.zip";

            string targetFolder = Path.Combine(testContextInstance.DeploymentDirectory, "targetFolder_noExists");

            bool doBackup = false;
            FileHelper.Unzip(zipFile, targetFolder, doBackup);

        }

        [TestMethod()]
        [DeploymentItem("FileHelper\\sample.zip", "FileHelper")]
        [ExpectedException(typeof(System.ArgumentException))]
        public void UnzipArgumentException_ZipNoExists_Test()
        {
            string zipFile = string.Empty;

            string targetFolder = Path.Combine(testContextInstance.DeploymentDirectory, "targetFolder");
            if (Directory.Exists(targetFolder))
                Directory.Delete(targetFolder, true);
            Directory.CreateDirectory(targetFolder);

            bool doBackup = false;
            FileHelper.Unzip(zipFile, targetFolder, doBackup);

        }

        [TestMethod()]
        [DeploymentItem("FileHelper\\sample.zip", "FileHelper")]
        [DeploymentItem("FileHelper\\sample.zip", "targetFolder")]
        public void UnzipBackupTest()
        {
            string zipFile = testContextInstance.DeploymentDirectory + "\\FileHelper\\sample.zip";
            string targetFolder = Path.Combine(testContextInstance.DeploymentDirectory, "targetFolder");

            bool doBackup = true;
            bool actual = FileHelper.Unzip(zipFile, targetFolder, doBackup);

            Assert.IsTrue(actual);
            Assert.IsTrue(Directory.GetFiles(targetFolder).Count() == 1);
            Assert.IsTrue(Directory.GetFiles(targetFolder, "sample.zip").Count() == 0);

            Directory.Delete(targetFolder, true);
        }
    }
}
